package com.oauth.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.appointmentregister.sina.Weibo;

/**
 * 获取配置文件
 * 
 * @author bywyu
 * 
 */
public class ConfigUtil {
	public static String weiboFlag;

	private static ConfigUtil instance;

	public static final String OAUTH_VERIFIER_URL = "oauth_verifier_url";

	public static boolean renrenReAuth = false;

	private String curWeibo = "";
	private String appKey = "";
	private String appSecret = "";
	private String open_url = "";
	private String request_token_url = "";
	private String authoriz_token_url = "";
	private String access_token_url = "";

	private String sinaAccessOauthToken = "";
	private String sinaAccessOauthTokenSecret = "";

	public void setSinaAccessOAuthToken(String str) {
		sinaAccessOauthToken = str;
	}

	public String getSinaAccessOAuthToken() {
		return sinaAccessOauthToken;
	}

	public void setSinaAccessOAuthTokenSecret(String str) {
		sinaAccessOauthTokenSecret = str;
	}

	public String getSinaAccessOAuthTokenSecret() {
		return sinaAccessOauthTokenSecret;
	}

	private String qqAccessOauthToken = "";
	private String qqAccessOauthTokenSecret = "";

	public void setQqAccessOAuthToken(String str) {
		qqAccessOauthToken = str;
	}

	public String getQqAccessOAuthToken() {
		return qqAccessOauthToken;
	}

	public void setQqAccessOAuthTokenSecret(String str) {
		qqAccessOauthTokenSecret = str;
	}

	public String getQqAccessOAuthTokenSecret() {
		return qqAccessOauthTokenSecret;
	}

	public static final String SINAW = "sina";
	public static final String QQW = "qq";

	// public static String callBackUrl = "www.baidu.com";
	public static String callBackUrl = "http://szicity.com";
	// public static String callBackUrl = "null";

	// --------------------qq
	private final String qq_AppKey = "801075386";
	private final String qq_AppSecret = "dfc583cdd7ebd7ca0f575d8a11acdf01";
	private final String qq_open_url = "https://open.t.qq.com/";
	private final String qq_Request_token_url = "https://open.t.qq.com/cgi-bin/request_token";
	private final String qq_Authoriz_token_url = "https://open.t.qq.com/cgi-bin/authorize";
	private final String qq_Access_token_url = "https://open.t.qq.com/cgi-bin/access_token";

	// ---------------------sina
	private final String sina_AppKey = "2932222159";
	private final String sina_AppSecret = "b14395ca4c5242e2d17c05b4afb260b8";
	private final String sina_open_url = "http://api.t.sina.com.cn/";
	private final String sina_Request_token_url = "http://api.t.sina.com.cn/oauth/request_token";
	private final String sina_Authoriz_token_url = "http://api.t.sina.com.cn/oauth/authorize";
	private final String sina_Access_token_url = "http://api.t.sina.com.cn/oauth/access_token";

	public static synchronized ConfigUtil getInstance() {
		if (instance == null) {
			instance = new ConfigUtil();
		}
		return instance;
	}

	public ConfigUtil() {

	}

	/**
	 * 初始化QQ认证信息
	 */
	public void initQqData() {
		weiboFlag = QQW;
		setAppKey(qq_AppKey);
		setAppSecret(qq_AppSecret);
		setOpenUrl(qq_open_url);
		setRequest_token_url(qq_Request_token_url);
		setAuthoriz_token_url(qq_Authoriz_token_url);
		setAccess_token_url(qq_Access_token_url);
	}

	/**
	 * 初始化SINA认证信息
	 */
	public void initSinaData() {
		weiboFlag = SINAW;
		Weibo weibo = Weibo.getInstance();
		weibo.setupConsumerConfig(sina_AppKey, sina_AppSecret);
		setAppKey(sina_AppKey);
		setAppSecret(sina_AppSecret);
		setOpenUrl(sina_open_url);
		setRequest_token_url(sina_Request_token_url);
		setAuthoriz_token_url(sina_Authoriz_token_url);
		setAccess_token_url(sina_Access_token_url);
	}

	public String getCurWeibo() {
		return curWeibo;
	}

	/**
	 * 设置当前操作的weibo 不同的weibo请求存在�?���?
	 * 
	 * @param curWeibo
	 */
	public void setCurWeibo(String curWeibo) {
		this.curWeibo = curWeibo;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getRequest_token_url() {
		return request_token_url;
	}

	public void setRequest_token_url(String requestTokenUrl) {
		request_token_url = requestTokenUrl;
	}

	public String getAuthoriz_token_url() {
		return authoriz_token_url;
	}

	public void setAuthoriz_token_url(String authorizTokenUrl) {
		authoriz_token_url = authorizTokenUrl;
	}

	public String getAccess_token_url() {
		return access_token_url;
	}

	public void setAccess_token_url(String accessTokenUrl) {
		access_token_url = accessTokenUrl;
	}

	public void setOpenUrl(String openUrl) {
		open_url = openUrl;
	}

	public String getOpenUrl() {
		return open_url;
	}
	
	private static final String TOKEN_SHARE = "token_share";
	private static final String SINA_ACCESS = "sina_access";
	
	/**
	 * 创建微博SharePreference
	 * @param c
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context c){
		return c.getSharedPreferences(TOKEN_SHARE, 0);
	}

	/**
	 * 获取新浪微博access_token
	 * @param c
	 * @return
	 */
	public static String getSinaToken(Context c){
		return getSharedPreferences(c).getString(SINA_ACCESS, null);
	}

	
	/**
	 * 存储新浪微博access包含access_token,uid和过期时间，以“&”连接
	 * @param c
	 * @param token
	 */
	public static void saveSinaToken(Context c,String token){
		SharedPreferences.Editor localEditor = getSharedPreferences(c).edit();
		localEditor.putString(SINA_ACCESS, token);
		localEditor.commit();
	}
	
	/**
	 * 删除新浪微博access_token
	 * @param c
	 */
	public static void delSinaToken(Context c){
		SharedPreferences.Editor localEditor = getSharedPreferences(c).edit();
		localEditor.remove(SINA_ACCESS);
		localEditor.commit();
	}
	
}
